const {
	Controller
} = require('uni-cloud-router')
const config = require("../config-uni-id-member.js")
const uniID = require('uni-id')

const getUniID = (context) => {
	return uniID.createInstance({
		context,
		config
	})
}

module.exports = class MemberController extends Controller {
	async loginByWeixin() {
		const {
			wxMobileData,
			code
		} = this.ctx.data
		try {
			let result = await this.service.member.loginByWeixin({
				code,
				wxMobileData
			})
			return {
				code: 200,
				result
			}
		} catch (e) {
			console.log(e)
			return {
				code: 500,
				msg:  e.message
			}
		}
	};
	
	
	
	
	
	
	
	async loginByToutiao() {
		const {
			code,
			ttMobileData
		} = this.ctx.data
		try {
			let result = await this.service.member.loginByToutiao({
				code,
				ttMobileData
			})
			return {
				code: 200,
				result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}

	async loginWithPwd() {
		const {
			username,
			password
		} = this.ctx.data
		return this.service.member.loginWithPwd({
			username,
			password
		})
	}

	async checkToken() {
		const {
			token
		} = this.ctx.data
		try {
			let payload = await getUniID(this.ctx.context).checkToken(token)
			if (!payload.code) {
				return {
					code: 200,
					result: true
				}
			} else {
				return {
					code: 200,
					result: false
				}
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}

	async getHomeData() {
		let {
			token,
			userLocation,
			city
		} = this.ctx.data
		try {
			if (userLocation && (isNaN(parseFloat(userLocation.latitude)) || isNaN(parseFloat(userLocation.longitude)))) {
				userLocation = null
			}
			let startTime = Date.now()
			let result = {}
			let region = await this.service.region.getNeareastCity({
				userLocation
			})
			result.region = region

			let payload = await getUniID(this.ctx.context).checkToken(token)
			if (!payload.code) {
				const res = await getUniID().getUserInfo({
					uid: payload.uid,
					field: ['wx_unionid', 'shareGrade']
				})
				if (!res.code) {
					let wxPubUser = await this.service.wxPublicUser.getWxPubUser({
						city: city != null ? city : region.city.value,
						unionId: res.userInfo.wx_unionid
					})
					result.isWxPubUser = wxPubUser != undefined

					if (res.userInfo.shareGrade) {
						result.notices = await this.service.notice.getNotices({
							city: city != null ? city : region.city.value,
							eredar: true
						})
					} else {
						result.notices = await this.service.notice.getNotices({
							city: city != null ? city : region.city.value,
							eredar: false
						})
					}
				}
			}
			let categories = await this.service.goodsCategory.getCategories({
				rootCateName: '优惠平台'
			})
			result.categories = categories
			result.goods = await this.service.goods.getGoodsListPub({
				pageNum: 1,
				pageSize: 10,
				filter: {
					displayOnHome: true
				},
				order: {
					orderNo: 'asc'
				},
				city: city != null ? city : region.city.value,
				userLocation,
				token
			})
			let endTime = Date.now()
			console.log('cost: ', endTime - startTime)
			return {
				code: 200,
				result
			}
		} catch (e) {
			console.error(e)
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//获取剧本车队列表
	async getSubscribeList() {
		let {
			city,
			userLocation
		} = this.ctx.data
		try {
			if (!city || !userLocation) {
				return {
					code: 500,
					msg: "参数错误"
				}
			}
			let res = await this.service.subscribe.getSubscribeList({
				city,
				userLocation
			})
			return {
				code: 200,
				result: res
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//获取门店列表
	async getStoreList() {
		let {
			city,
			userLocation
		} = this.ctx.data
		try {
			if (!city || !userLocation) {
				return {
					code: 500,
					msg: "参数错误"
				}
			}
			let res = await this.service.subscribe.getStoreList({
				city,
				userLocation
			})
			return {
				code: 200,
				result: res
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//获取商品车队列表（pageNum = 0首页的车队信息）
	async getGoodsMotorcadeList() {
		let {
			pageNum = 0,
			pageSize = 10,
			categoryId,
			userLocation,
			filter
		} = this.ctx.data
		try{
			if(pageNum > 0) {
				if (!userLocation) {
					return {
						code: 500,
						msg: '参数错误'
					}
				}
			}
			let result = await this.service.order.getGoodsMotorcadeList({
				pageNum,
				pageSize,
				categoryId,
				userLocation,
				filter
			})
			return {
				code: 200,
				result: result
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
}
